package com.example.tagsdemo.handler.biz;

import com.example.tagsdemo.annotation.HandlerType;
import com.example.tagsdemo.dto.GeneratorDTO;
import com.example.tagsdemo.entity.DBTable;
import com.example.tagsdemo.entity.DBTableColumns;
import com.example.tagsdemo.enums.ResultEnum;
import com.example.tagsdemo.handler.AbstractHandler;
import com.example.tagsdemo.service.DBService;
import com.example.tagsdemo.utils.ResultVOUtil;
import com.example.tagsdemo.utils.generator.TKFileUtils;
import com.example.tagsdemo.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.Executor;

@Component
@HandlerType("tk")
@Slf4j
public class TKHandler extends AbstractHandler {

    @Autowired
    private Executor taskExecutor;

    @Autowired
    private DBService service;

    @Override
    public ResultVO handle(GeneratorDTO dto) {
        String[] tableArr = StringUtils.split(dto.getTableArr(),",");
        List<DBTable> tables = service.getTables();
        for (String t: tableArr) {
            try {
                DBTable table = getTables(tables,t);
                List<DBTableColumns> columns = table.getList();

                String basePackage = dto.getEntityPath().substring(0,dto.getEntityPath().lastIndexOf(".")) + ".base";

                taskExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        TKFileUtils.writerBaseData(dto.getWorkPath(),basePackage);
                    }
                });

                taskExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        TKFileUtils.readEntity( t,table.getRemarks(), basePackage,dto,columns);
                    }
                });

                taskExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        TKFileUtils.readService(t,basePackage,dto);

                    }
                });
                taskExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        log.info(Thread.currentThread().getName());
                        TKFileUtils.readServiceImpl(t,table.getRemarks(),basePackage,dto);
                    }
                });
                taskExecutor.execute(new Runnable() {
                    @Override
                    public void run() {
                        log.info(Thread.currentThread().getName());
                        TKFileUtils.readDao(t,basePackage,dto,getIdType(columns));
                    }
                });
            }catch (Exception e){
                log.error("create entitys fail,{}",t);
                return ResultVOUtil.error(ResultEnum.CREATE_ENTITY_ERROR);
            }
        }
        return ResultVOUtil.success();
    }


}
